<template>
  <div>
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" style="width:800px;height: 600px">
      <el-form-item label="账号名" prop="username">
        <el-input v-model="form.username"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input type="password" v-model="form.password"></el-input>
      </el-form-item>
      <el-form-item label="请确认密码" prop="password_two">
        <el-input type="password" v-model="form.password_two"></el-input>
      </el-form-item>
      <el-form-item label="昵称" prop="nickname">
        <el-input v-model="form.nickname"></el-input>
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="form.email"></el-input>
      </el-form-item>
      <el-form-item label="电话" prop="phone">
        <el-input v-model="form.phone"></el-input>
      </el-form-item>
      <el-form-item class="bottom">
        <router-link class="link" to="/">已有账号？</router-link>
        <el-button type="primary" @click="submitForm">注册</el-button>
        <el-button @click="resetForm">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import axios from "axios";

export default {
  data() {
    return {
      form: {
        username: '',
        password: '',
        password_two: '',
        nickname: '',
        email: '',
        phone: ''
      },
      rules: {
        username: [{ required: true, message: '请输入账号名', trigger: 'blur' }],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
        password_two: [{ required: true, message: '请确认密码', trigger: 'blur' },
          { validator: (rule, value, callback) => {
              if (value === '') {
                callback(new Error('请再次输入密码'))
              } else if (value !== this.form.password) {
                callback(new Error('两次输入密码不一致!'))
              } else {
                callback()
              }
            }, trigger: 'blur' }],
        email: [
          { required: true, message: '请输入邮箱', trigger: 'blur' },
          { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] }
        ],
        phone: [
          { required: true, message: '请输入电话号码', trigger: 'blur' },
          { pattern: /^1[0-9]{10}$/, message: '请输入正确的电话号码', trigger: ['blur', 'change'] }
        ]
      }
    }
  },
  methods: {
    submitForm() {
      this.$refs.form.validate(valid => {
        if (valid) {
          axios({
            method: 'post',
            url: 'http://localhost:5000/usr/register',
            params: {
              username: this.form.username,
              password: this.form.password,
              nickname: this.form.nickname,
              email: this.form.email,
              phone: this.form.phone
            }
          }).then(res => {
            console.log(res)
            if (res.data.code === 200) {
              this.$message({
                message: '注册成功',
                type: 'success'
              })
              localStorage.clear()
              localStorage.setItem('user_id', res.data.user_id)
              let baseInfo=res.data.base_info;
              localStorage.setItem('email', baseInfo.email)
              localStorage.setItem('phone', baseInfo.phone)
              localStorage.setItem('nickname', baseInfo.nickname)
              localStorage.setItem('avator', baseInfo.avator)
              localStorage.setItem('cost', baseInfo.cost)
              localStorage.setItem('rate', baseInfo.rate)
              localStorage.setItem('risk', baseInfo.risk)
              localStorage.setItem('time', baseInfo.time)
              this.$router.push('/home')
            } else {
              this.$message({
                message: '注册失败',
                type: 'error'
              })
            }
          }).catch(err => {
            this.$message({
              message: '注册失败',
              type: 'error'
            })
          })
        } else {
          // 表单校验失败
          console.log('表单校验失败')
          return false
        }
      })
    },
    resetForm() {
      this.$refs.form.resetFields()
    }
  }
}
</script>

<style lang="scss" scoped>
//*{
//  width: 200%;
//}
//调整表格宽度
.el-input{
  width: 90%;
}

.bottom{
  translate: -80px 50px;
}

.link{
  translate: -180px;
}
</style>